home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / appl / napsaterm / fonts.c < prev    next >
C/C++ Source or Header  |  1994-05-14  |  6KB  |  302 lines

  1. RCS_ID_C="$Id: fonts.c,v 3.2 1994/02/25 02:33:38 ppessi Exp $";
  2. /*
  3.  * fonts.c - Amiga font handling routines
  4.  *
  5.  * Authors: Pekka Pessi <Pekka.Pessi@hut.fi>,
  6.  *          original from Niftyterm by Chris Newman, Todd Williamson.
  7.  *
  8.  * Copyright (c) 1989, 1993 by authors.  All Rights Reserved
  9.  *
  10.  * Permission is granted to copy, modify, and use this as long
  11.  * as this notice remains intact.  This is a nifty program.
  12.  *
  13.  * DISCLAIMER: the authors of this program are not responsible
  14.  * for any damage or other problems caused by it.
  15.  *
  16.  * Created      : Sat Nov 13 06:55:40 1993 ppessi
  17.  * Last modified: Fri Feb 25 02:09:10 1994 ppessi
  18.  *
  19.  * $Log: fonts.c,v $
  20.  * Revision 3.2  1994/02/25  02:33:38  ppessi
  21.  * Added RCS ID
  22.  *
  23.  * Revision 3.1  1994/01/07  22:51:18  ppessi
  24.  * Version 3 beta
  25.  *
  26.  * Revision 2.3  93/11/24  18:19:58  ppessi
  27.  * Fixed some fontname problems..
  28.  * 
  29.  * Revision 2.2  93/11/18  18:35:13  ppessi
  30.  * *** empty log message ***
  31.  * 
  32.  * Revision 2.1  93/11/17  15:45:25  ppessi
  33.  * Changed function names to more logical, restructured
  34.  * the role of rastport pointer and fonts.
  35.  * 
  36.  * Revision 2.0  93/11/15  03:43:43  ppessi
  37.  * Version 2 initial revision
  38.  * 
  39.  */
  40.  
  41. #include <assert.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44.  
  45. #include "nifty.h"
  46. #include "display.h"
  47. #include "dispmacros.h"
  48. #include "napsaprefs.h"
  49.  
  50. #include "amiga.h"
  51. #include "wimp.h" 
  52.  
  53. #if USE_PRAGMAS
  54. #include <proto/diskfont.h>
  55. #endif
  56. #if USE_INLINE
  57. #include <inline/diskfont.h>
  58. #endif
  59. #if USE_CLIB
  60. #include <clib/diskfont_protos.h>
  61. #endif
  62.  
  63. /* I didn't want to have to type this  */
  64. #define tf_Name tf_Message.mn_Node.ln_Name
  65.  
  66. /* array of fonts */
  67. static struct {
  68.   struct TextFont *attr;    /* font pointer */
  69.   ULONG            style;    /* style to use with this font */
  70. } fonts[ATTRIB_MASK + 1];
  71.  
  72. /*
  73.  * Font name information
  74.  */
  75. static struct font_names {
  76.   char *base;        /* main font name */
  77.   char *alt;        /* alternate character set font (as, ae) */
  78.   char *wide;        /* wide font */
  79.   char *top;        /* top of double height */
  80.   char *bot;        /* bottom of double height */
  81.   char *altwide;    /* alternate wide font */
  82.   char *alttop;        /* alternate top font */
  83.   char *altbot;        /* alternate bottom font */
  84.   char *narrow;        /* for 132 column mode */
  85. } font_names;
  86.  
  87. #define NO_OF_FONTS (sizeof(font_names)/sizeof(char*))
  88.  
  89. static char fontpostfixes[] =
  90.   ".font"  NUL "v.font"  NUL "w.font"  NUL "t.font"  NUL 
  91.   "b.font" NUL "vw.font" NUL "vt.font" NUL "vb.font" NUL 
  92.   "n.font" NUL;
  93.  
  94. /*
  95.  * Load in font #i, return true if succesfull
  96.  */
  97. static int LoadFont(ULONG i)
  98. {
  99.   char    *fontname;
  100.   int        retries = 0, mem = 0, disk = 0;
  101.   struct TextAttr ta;
  102.   struct TextFont *tf = NULL, *tfd = NULL;
  103.  
  104.   i &= ATTRIB_MASK;
  105.   switch (i & (DOUBLE1 | DOUBLE2 | ALTERNATE)) {
  106.   case DOUBLE1 | DOUBLE2:
  107.     fontname = font_names.wide;
  108.     break;
  109.  
  110.   case DOUBLE1 | DOUBLE2 | ALTERNATE:
  111.     fontname = font_names.altwide;
  112.     break;
  113.  
  114.   case DOUBLE1:
  115.     fontname = font_names.top;
  116.     break;
  117.  
  118.   case DOUBLE1 | ALTERNATE:
  119.     fontname = font_names.alttop;
  120.     break;
  121.  
  122.   case DOUBLE2:
  123.     fontname = font_names.bot;
  124.     break;
  125.  
  126.   case DOUBLE2 | ALTERNATE:
  127.     fontname = font_names.altbot;
  128.     break;
  129.  
  130.   case ALTERNATE:
  131.     fontname = font_names.alt;
  132.     break;
  133.  
  134.   default:
  135.     fontname = font_names.base;
  136.     break;
  137.   }
  138.  
  139.   ta.ta_YSize = wm.font.h;
  140.   ta.ta_Style = (i & STYLE_MASK);
  141.   ta.ta_Flags = 0;
  142.  
  143.   do {
  144.     ta.ta_Name = (STRPTR)fontname;
  145.  
  146.     /* Is it already loaded? */
  147.     if (tf = OpenFont(&ta)) 
  148.       if (!lstrncmp(tf->tf_Name, fontname, strlen(tf->tf_Name))) 
  149.     if (tf->tf_YSize == ta.ta_YSize) {
  150.       mem = 1;        /* Maybe? */
  151.       if (tf->tf_Style == ta.ta_Style) 
  152.         mem = 2;        /* Perfect! */
  153.     }
  154.  
  155.     /* Nope, but perhaps it is on disk? */
  156.     if ((mem < 2) && (tfd = OpenDiskFont(&ta))) 
  157.       if (!lstrncmp(tfd->tf_Name, fontname, strlen(tfd->tf_Name))) 
  158.     if (tfd->tf_YSize == ta.ta_YSize) 
  159.       disk = 1;
  160.     
  161.     if (disk || mem) {
  162.       if (disk && mem) 
  163.     if (tfd->tf_Style > tf->tf_Style) 
  164.       mem = 0;
  165.       /* */
  166.       if (!mem) {
  167.     if (tf) CloseFont(tf);
  168.     tf = tfd;
  169.       } else
  170.     if (tfd) CloseFont(tfd);
  171.  
  172.       fonts[i].style = (~(tf->tf_Style) & ta.ta_Style);
  173.       fonts[i].attr = tf;
  174.       return 1;
  175.     }
  176.  
  177.     if (tf) {
  178.       CloseFont(tf);
  179.       tf = NULL;
  180.     }
  181.  
  182.     if (tfd) {
  183.       CloseFont(tfd);
  184.       tfd = NULL;
  185.     }
  186.  
  187.     if (i & ALTERNATE && retries == 0)
  188.       fontname = font_names.base;
  189.     else if (i & ALTERNATE && retries == 1)
  190.       fontname = DEFAULTFONT "v.font";
  191.     else if (retries == 0)
  192.       fontname = DEFAULTFONT ".font", retries++;
  193.     else {
  194.       fontname = "topaz.font";
  195.       ta.ta_YSize = 8;
  196.     }
  197.   } while (++retries <= 3);
  198.  
  199.   /* Set at least something */
  200.   fonts[i].attr = fonts[0].attr;
  201.  
  202.   /* Give up */
  203.   return 0;
  204. }
  205.  
  206. /*
  207.  * Set fontnames
  208.  */
  209. static void setbasefont(char *name)
  210. {
  211.   char *c, *t, *posts, **namep;
  212.   long height = 11;
  213.   
  214.   int DEBUGSIZE;
  215.  
  216.   c = FilePart(name);
  217.   if (c == name) {
  218.     height = DEFAULTFONTHEIGHT;
  219.   } else {
  220.     height = DEFAULTFONTHEIGHT;
  221.     StrToLong(c, &height);
  222.     wm.font.h = height;
  223.  
  224.     c = PathPart(name);
  225.     c[0] = '\0';    
  226.   }
  227.  
  228.   c = malloc(DEBUGSIZE = strlen(name) * NO_OF_FONTS + sizeof(fontpostfixes));
  229.  
  230.   if (!c) {
  231.     fatalError(MEMORY_ERROR_MSG);
  232.   }
  233.  
  234.   posts = fontpostfixes;
  235.   namep = &font_names.base; 
  236.  
  237.   do {
  238.     *namep++ = c;
  239.     /* Copy basename */
  240.     for (t = name; *c = *t; t++, c++)
  241.       ;
  242.     /* concat prefix */
  243.     for (; *c++ = *posts++; t++)
  244.       ;
  245.   } while (*posts);
  246.  
  247.   assert(c <= font_names.base + DEBUGSIZE);
  248. }
  249.  
  250. /*
  251.  * Initialize fonts
  252.  */
  253. void initfonts(void)
  254. {
  255.   int        i;
  256.  
  257.   for (i = 0; i <= ATTRIB_MASK; i++) {
  258.     fonts[i].attr = NULL;
  259.     fonts[i].style = 0;
  260.   }
  261.  
  262.   /* Set the base font */
  263.   setbasefont(np.basefont);
  264.  
  265.   if (LoadFont(0L)) {
  266.     /* Initialize font dimensions for display routines */
  267.     struct TextFont *tf = fonts[0].attr;
  268.  
  269.     wm.font.w = tf->tf_XSize;
  270.     wm.font.h = tf->tf_YSize;
  271.     wm.font.b = tf->tf_Baseline;
  272.   } else {
  273.     /* Something is badly broken */
  274.     fatalError("Cannot load any font");
  275.   }
  276. }
  277.  
  278. /*
  279.  * Free loaded fonts
  280.  */
  281. void deinitfonts(void)
  282. {
  283.   int i;
  284.  
  285.   for (i = 0; i <= ATTRIB_MASK; i++) {
  286.     if (fonts[i].attr)
  287.       CloseFont(fonts[i].attr);
  288.     fonts[i].attr = NULL;
  289.   }
  290. }
  291.  
  292. void setfont(register struct RastPort *rastport, register ULONG style)
  293. {
  294.   if (!fonts[style].attr)
  295.     LoadFont(style);
  296.  
  297.   SetFont(rastport, fonts[style].attr);
  298.  
  299.   SetSoftStyle(rastport, fonts[style].style, 0xffffffff);
  300. }
  301.  
  302.